package com.uam.core.config;

import com.uam.core.dao.mysql.PolygonV1Dao;
import com.uam.core.dao.mysql.TaskDao;
import com.uam.core.entity.constant.RedisConstants;
import com.uam.core.entity.constant.RedisLockKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * 监控任务完成
 * @author Administrator
 */
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private PolygonV1Dao polygonV1Dao;

    @Autowired
    private RedissonClient redisson;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 监控任务完成记录
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            String key=message.toString();
//            if (key.startsWith(RedisConstants.ROUTE_FINISH)) {
//                try {
//                    Long taskId = Long.valueOf(key.split("\\:")[1]);
//                    taskDao.completeTaskV2(taskId);
//                    polygonV1Dao.delete(taskId);
//                    log.info("key:{},任务完成Id:{}  ", key,taskId);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
            if (key.startsWith(RedisConstants.ROUTE_AIR_TASK)) {
                Long taskId = Long.valueOf(key.split("\\:")[1]);
                taskDao.completeTaskV2(taskId);
            }
        }catch (Exception e){
            log.error("监控任务逻辑处理失败：{}",message.toString(),e);
        }
    }

}

